﻿
/*----------------------------------------------------------------
 * 创建时间：2023/9/4 14:58:12
 * 作  者：wenlli 
 * 项  目：Minotaur.App.Core.Attribute 
 * 描  述：
 *
 * ----------------------------------------------------------------
 * 修改人：
 * 时  间：
 * 修改说明：
 * 
 *----------------------------------------------------------------*/


using AspectInjector.Broker;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Linq;
using System.Linq;
using Minotaur.App.Domains.Model.SystemLog;
using System.Net;
using System.Linq;
using System.Windows;
using System.Xml.Linq;
using ServiceStack;
using ServiceStack.Script;

namespace Minotaur.App.Core.Attributes
{
    [Aspect(Scope.Global)]
    [Injection(typeof(GlobalLogAspectAttribute))]
    public class GlobalLogAspectAttribute : Attribute
    {
        private GlobalUserModel _userModel => AppDataContext.GlobalUserModel;
        private IDbContext _dbContext => AppDataContext.dbContext;



        /// <summary>
        /// 执行前
        /// </summary>
        /// <param name="name">方法名</param>
        /// <param name="arguments">参数</param>
        [Advice(Kind.Before)]
        public void LogBefore([Argument(Source.Name)] string name, [Argument(Source.Arguments)] object[] arguments, [Argument(Source.Type)] Type hostType)
        {
            Console.WriteLine($"Before，调用方法：'{name}'，调用参数：{JsonConvert.SerializeObject(arguments)}");
            Insert(name, arguments, null, hostType);
        }
        /// <summary>
        /// 获取本地IP地址信息
        /// </summary>
        string GetAddressIP()
        {
            ///获取本地的IP地址
            string AddressIP = string.Empty;
            foreach (IPAddress _IPAddress in Dns.GetHostEntry(Dns.GetHostName()).AddressList)
            {
                if (_IPAddress.AddressFamily.ToString() == "InterNetwork")
                {
                    AddressIP = _IPAddress.ToString();
                    break;
                }
            }
            return AddressIP;
        }
        /// <summary>
        /// 执行后
        /// </summary>
        /// <param name="name">方法名</param>
        /// <param name="arguments">参数</param>
        /// <param name="retrrnValue">返回值</param>
        [Advice(Kind.After)]
        public void LogAfter([Argument(Source.Name)] string name, [Argument(Source.Arguments)] object[] arguments, [Argument(Source.ReturnValue)] object retrrnValue, [Argument(Source.Type)] Type hostType)
        {
            Insert(name, arguments, retrrnValue, hostType);
            Console.WriteLine($"After，调用方法：'{name}'，调用参数：{JsonConvert.SerializeObject(arguments)}，返回值为：{JsonConvert.SerializeObject(retrrnValue)}");
        }

        void Insert(string name, object[] arguments, object retrrnValue, Type hostType)
        {
            SystemRequestLog systemRequestLog = new SystemRequestLog()
            {
                Account = this._userModel.Account,
                CNName = this._userModel.EmpName,
                Code = this._userModel.TenementCode,
                CreatDate = DateTime.Now,
                Creator = this._userModel.EmpId,
                EmpCode = this._userModel.EmpCode,
                EmpId = this._userModel.EmpId,
                ENName = this._userModel.EmpName,
                ExecuteCompletiontime = DateTime.Now,
                ExecuteDate = DateTime.Now,
                ExecuteMethod = $"方法:【{name}】:调用{(retrrnValue is null ? "前" : "后")}执行",
                ExecuteParameter = JsonConvert.SerializeObject(arguments),
                IPAddress = GetAddressIP(),
                Language = 0,
                LogType = 2,
                ExecuteResult = retrrnValue is null ? "" : JsonConvert.SerializeObject(retrrnValue),
                ExecuteClass = hostType.FullName,
                PositionId = this._userModel.PositionId,
                ModifDate = DateTime.Now,
                Modifier = this._userModel.EmpId,
                OrgId = this._userModel.OrgId,
                OrgName = this._userModel.OrgName,
                Phone = "",
                PositionName = this._userModel.PositionName,
                TCnname = this._userModel.TenementName,
                UserAgent = "Windows desktop",
                RequestParamtetr = JsonConvert.SerializeObject(arguments),
            };
            this._dbContext.MasterDb.Insertable<SystemRequestLog>(systemRequestLog).ExecuteCommand();
        }
    }
}
